package cn.lf.android.lawfirm.distribution.controller;


import android.content.Context;

import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.distribution.modle.Distribution;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.JsonBeans;
import cn.lf.android.lawfirm.utils.SPUtil;

/**
 * 列表
 */
public class DistributionListConroller extends BaseController {
    DistributionListCallback mDistributionListCallback;
    private Context context;

    public DistributionListConroller(DistributionListCallback callback, DistributionDeleteCallback callback1, Context context) {
        this.mDistributionListCallback = callback;
        this.mDistributionDeleteCallback = callback1;
        this.context = context;
    }

    DistributionDeleteCallback mDistributionDeleteCallback;


    /**
     * 列表
     *
     * @param q         案件名称或案件合同号，自动联想参数（非必填，长度100）
     * @param pageIndex
     * @param state
     */
    public void onDistributList(String q, String pageIndex, String state) {
        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("q", q);
        params.put("pageIndex", pageIndex);
        params.put("state", state);
        params.put("Authorization", spUtil.getAccessToken());
        DebugLog.e("Authorization==" + spUtil.getAccessToken());
        DebugLog.e("pageIndex==" + pageIndex);
        DebugLog.e("state==" + state);
        new MyOkHttpClient().doGet(AppConfig.CaseInfo_dispatched, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("分派列表==" + response);
                        JSONObject myJsonObject;
                        JSONObject responsObject;
                        String arraylist;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                responsObject = myJsonObject.getJSONObject("responsObject");
                                arraylist = responsObject.getJSONArray("result")
                                        .toString();
                                List<Distribution> beans = JsonBeans
                                        .getJsonList(
                                                arraylist,
                                                new TypeToken<List<Distribution>>() {
                                                });
                                mDistributionListCallback.onDistributionListuccessed(beans);
                            } else {
                                mDistributionListCallback
                                        .onDistributionListFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        mDistributionListCallback.onDistributionListFailed();
                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    /**
     * 删除案源分派
     *
     * @param caseId
     */
    public void onDeleteDistribut(String caseId) {
        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("caseId", caseId);
        DebugLog.e("caseId==" + caseId);
        new MyOkHttpClient().doPost(AppConfig.CaseInfo_Delete, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("删除案源分派==" + response);
                        JSONObject myJsonObject;
                        JSONObject responsObject;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                mDistributionDeleteCallback.onDistributionDeleteSuccessed("");
                            } else {
                                mDistributionDeleteCallback
                                        .onDistributionDeleteFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        DebugLog.e("删除案源分派失败==" + errorMsg);
                        mDistributionDeleteCallback.onDistributionDeleteFailed();
                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    public interface DistributionListCallback {
        public void onDistributionListuccessed(List<Distribution> beans);

        public void onDistributionListFailed();

    }

    public interface DistributionDeleteCallback {
        public void onDistributionDeleteSuccessed(String beans);

        public void onDistributionDeleteFailed();

    }
}
